home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr25 / startd.zip / STARTD.C < prev    next >
C/C++ Source or Header  |  1993-03-26  |  9KB  |  273 lines

  1. /*
  2.  *  STARTD:  A simple program to start a DOS session under OS/2 2.0.  
  3.  *           This program can be run from an OS/2 command prompt
  4.  *
  5.  *  Last Modfied: 07/07/92
  6.  *
  7.  *  Author: Norm Ross
  8.  *  
  9.  *  Using the example code for BOOTA.C found in IBM DOCUMENT GBOF-2254
  10.  *
  11.  *  This was written mainly to provide access to DOS SETTINGS for
  12.  *  VDM sessions. Since I have disabled the WPS to free up the space,
  13.  *  I can't specify DOS settings for my favorite DOS apps.
  14.  *  STARTD is not meant to replace START but it does many of
  15.  *  the same things...
  16.  *
  17.  *  If you change this program and re-distribute it please leave this
  18.  *  header intact and send the readme file with it.
  19.  * 
  20.  *  MODIFICATION HISTORY
  21.  *   
  22.  *  07-Jul-1992  Norm Ross, npross@undergrad.uwaterloo.ca
  23.  *     1.00 : Initial version
  24.  */
  25.  
  26. #define INCL_DOSSESMGR
  27. #define INCL_DOSMISC
  28. #define INCL_DOSPROCESS
  29. #include <os2.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32.  
  33. PBYTE readEnv(PSZ pFileName);
  34.  
  35. PSZ pBootFailure = "Session could not be started.\r\n";
  36. char szPgmInputs[256];
  37. char szDosPgmInputs[256];
  38.  
  39. STARTDATA startd;                  /* Session start information */
  40. ULONG SessionID;                   /* Session and Process ID for new session*/
  41. PID ProcessID;
  42.  
  43. void main(int argc, char *argv[])
  44. {
  45.   USHORT       rc;
  46.   USHORT       flagWin = 0, flagFs = 0, flagKeep = 0, flagNoCmd = 0;
  47.  
  48.   /* ---- init startd struct defaults */
  49.   startd.Length                   = sizeof(STARTDATA);
  50.   startd.Related                  = SSF_RELATED_INDEPENDENT;
  51.   startd.FgBg                     = SSF_FGBG_FORE;
  52.   startd.TraceOpt                 = SSF_TRACEOPT_NONE;
  53.   startd.PgmTitle                 = NULL;
  54.   startd.PgmName                  = NULL;
  55.   startd.PgmInputs                = NULL;
  56.   startd.TermQ                    = NULL;
  57.   startd.Environment              = NULL;
  58.   startd.InheritOpt               = SSF_INHERTOPT_PARENT;
  59.   startd.SessionType              = SSF_TYPE_DEFAULT;
  60.   startd.PgmControl               = SSF_CONTROL_VISIBLE;
  61.  
  62.  
  63.   /* ------ Process args */
  64.   while(--argc > 0) {
  65.     char *arg;
  66.  
  67.     switch(**++argv) {
  68.     case '\"':
  69.     startd.PgmTitle = strtok(*argv, "\"");
  70.     break;
  71.     case '-':
  72.     case '/':
  73.       arg = (*argv)+1;
  74.       if (strlen(arg) == 1) {
  75.     switch(*arg) {
  76.     case 'n':
  77.     case 'N': flagNoCmd = 1; break;
  78.     case 'k':
  79.     case 'K': flagKeep = 1; break;
  80.     case 'c':
  81.     case 'C': break;
  82.     case 'f':
  83.     case 'F': startd.FgBg = SSF_FGBG_FORE; break;
  84.     case 'B':
  85.     case 'b': startd.FgBg = SSF_FGBG_BACK; break;
  86.     case 'i': 
  87.     case 'I': startd.InheritOpt = SSF_INHERTOPT_SHELL; break;
  88.     case '?': usage(); break;
  89.     default:
  90.       fprintf(stderr, "Unrecognized option: %s\n", arg);
  91.       usage();
  92.       break;
  93.     }
  94.       } else if (strcasecmp(arg, "DOS") == 0) { 
  95.     startd.SessionType = SSF_TYPE_VDM;
  96.       } else if (strcasecmp(arg, "WIN") == 0) {
  97.     flagWin = 1;
  98.       } else if (strcasecmp(arg, "WAIT") == 0) {
  99.     startd.Related = SSF_RELATED_CHILD;
  100.       } else if (strcasecmp(arg, "FS") == 0) {
  101.     flagFs = 1;
  102.       } else if (strcasecmp(arg, "FG") == 0) {
  103.     startd.FgBg = SSF_FGBG_FORE; 
  104.       } else if (strcasecmp(arg, "BG") == 0) {
  105.     startd.FgBg = SSF_FGBG_BACK;
  106.       } else if (strcasecmp(arg, "MAX") == 0) {
  107.     startd.PgmControl |= SSF_CONTROL_MAXIMIZE;
  108.       } else if (strcasecmp(arg, "MIN") == 0) {
  109.     startd.PgmControl |= SSF_CONTROL_MINIMIZE;
  110.       } else if (strcasecmp(arg, "INV") == 0) {
  111.     startd.PgmControl |= SSF_CONTROL_INVISIBLE;
  112.       } else if (strcasecmp(arg, "PM") == 0) {
  113.     startd.SessionType = SSF_TYPE_PM;
  114.       } else if (strncasecmp(arg, "pos", 3) == 0) {
  115.     char *s = strtok(arg, "=");
  116.  
  117.     /* ---- I really should check strtok's return... */
  118.     startd.PgmControl |= SSF_CONTROL_SETPOS;
  119.     startd.InitXPos = atoi(strtok(NULL, ","));
  120.     startd.InitYPos = atoi(strtok(NULL, ","));
  121.     startd.InitXSize = atoi(strtok(NULL, ","));
  122.     startd.InitYSize = atoi(strtok(NULL, ""));
  123.       } else if (strcasecmp(arg, "PGM") == 0) {
  124.     char *p = szPgmInputs;
  125.     /* ---- strip quotes from name if there are any */
  126.     startd.PgmName = strtok(*argv, "\"");
  127.       
  128.     /* ---- cat the rest of the args together to pass to pgm */
  129.     while (argc > 1) {
  130.       argc--;
  131.       argv++;
  132.       strcpy(p, *argv);
  133.       p += strlen(*argv);
  134.       *p++ = ' '; /* put spaces between the args */
  135.     }
  136.     *p = '\0';
  137.     startd.PgmInputs = szPgmInputs;
  138.     break;
  139.       } else if (strcasecmp(arg, "ICON") == 0) {
  140.     startd.IconFile = *++argv;
  141.     argc--;
  142.       } else if (strcasecmp(arg, "SF") == 0) {
  143.     char *fname = *++argv;
  144.     argc--;
  145.     if (access(fname, 0)) {
  146.       fprintf(stderr, "Session File %s not found\n", fname);
  147.     }
  148.     startd.Environment = readEnv(fname);
  149.       } else {
  150.     printf("Unrecognized option: %s\n", arg);
  151.     usage();
  152.       }
  153.       break;
  154.  
  155.     default: 
  156.     {
  157.       char *p = szPgmInputs;
  158.       startd.PgmName = *argv;
  159.       
  160.       /* ---- cat the rest of the args together to pass to pgm */
  161.       while (argc > 1) {
  162.         argc--;
  163.         argv++;
  164.         strcpy(p, *argv);
  165.         p += strlen(*argv);
  166.         *p++ = ' '; /* put spaces between the args */
  167.       }
  168.       *p = '\0';
  169.       startd.PgmInputs = szPgmInputs;
  170.       break;
  171.     }
  172.  
  173.     } /* switch */
  174.   } /* while */
  175.  
  176.   /* ------ Start thru Command processor */
  177.   if ((startd.PgmName!=NULL)&&(startd.SessionType!=SSF_TYPE_PM)&&(!flagNoCmd)){
  178.     if (flagKeep)
  179.       strcpy(szDosPgmInputs, "/k ");
  180.     else
  181.       strcpy(szDosPgmInputs, "/c ");
  182.     strcat(szDosPgmInputs, startd.PgmName);
  183.     strcat(szDosPgmInputs, " ");
  184.     strcat(szDosPgmInputs, startd.PgmInputs);
  185.     startd.PgmInputs = szDosPgmInputs;
  186.     startd.PgmName = NULL;
  187.   }
  188.       
  189.   /* ------ Set the correct session type */     
  190.   if (flagWin) {
  191.     switch(startd.SessionType) {
  192.     case SSF_TYPE_DEFAULT: startd.SessionType = SSF_TYPE_WINDOWABLEVIO; break;
  193.     case SSF_TYPE_VDM: startd.SessionType = SSF_TYPE_WINDOWEDVDM; break;
  194.     case SSF_TYPE_PM: break;
  195.     } 
  196.   } else if (flagFs) {
  197.     switch(startd.SessionType) {
  198.     case SSF_TYPE_DEFAULT: startd.SessionType = SSF_TYPE_FULLSCREEN; break;
  199.     case SSF_TYPE_VDM: break;
  200.     case SSF_TYPE_PM: break;
  201.     }
  202.   }
  203.  
  204.   /* ------ Start the Session */
  205.   rc = DosStartSession( &startd, &SessionID, &ProcessID );
  206.  
  207.   if(rc) {
  208.     /* ------ Print out failure message */
  209.     DosPutMessage(1,strlen(pBootFailure),pBootFailure);
  210.   } else if (startd.Related == SSF_RELATED_CHILD) {
  211.     /* ------ Wait for child to finish */
  212.     RESULTCODES resultcodes;
  213.     PID pid;
  214.  
  215.     /* ------ Shot in the dark... Please fix and tell me about it*/
  216.     DosWaitChild(1, 1, &resultcodes, &pid, ProcessID);
  217.   }
  218.     
  219.  
  220. return;
  221. }
  222.  
  223. #define MAXENV 4096
  224. PBYTE readEnv(PSZ fname) {
  225.   FILE *fptr;
  226.   PBYTE env = (PBYTE)malloc(MAXENV);
  227.   PBYTE p = env;
  228.  
  229.   fptr = fopen(fname, "r");
  230.   if (fptr == (FILE *)NULL) {
  231.     fprintf(stderr, "\nFile %s cannot be found\n");
  232.     exit(-1);
  233.   }
  234.  
  235.   while (fgets(p, 80, fptr)) {
  236.     p+=strlen(p);
  237.     *(p-1)='\0';
  238.     if (p>env + 4096) {
  239.       fprintf(stderr, "ERROR: too many settings\n");
  240.       fflush(stderr);
  241.       exit(-1);
  242.     }
  243.   }
  244.  
  245.   realloc(env, p-env);
  246.  
  247. return(env);
  248. }
  249.  
  250. usage( void ) {
  251.   fprintf(stderr, "STARTD VERSION 1.0 by Norm Ross Copyright (c) 1992\n\n");
  252.   fprintf(stderr, "startd [\"program title\"] [/BG /C /DOS /F /FS /I /ICON iconfile /INV /K /MAX\n\t /MIN /PGM POS=x,y,x1,y1 /SF settingsfile /WIN] [command ...]\n\n");
  253.   fprintf(stderr, "\t/B[G]\t start session in background\n");
  254.   fprintf(stderr, "\t/C\t close session upon completion\n");
  255.   fprintf(stderr, "\t/DOS\t start a dos session\n");
  256.   fprintf(stderr, "\t/F[G]\t start session in foreground\n");
  257.   fprintf(stderr, "\t/FS\t start a full screen session\n");
  258.   fprintf(stderr, "\t/I\t sets SSF_INHERTOPT_SHELL\n");
  259.   fprintf(stderr, "\t/ICON\t uses the specified icon file\n");
  260.   fprintf(stderr, "\t/INV\t start the application invisibly\n");
  261.   fprintf(stderr, "\t/K\t keep the session around after it is finished\n");
  262.   fprintf(stderr, "\t/MAX\t start maximized\n");
  263.   fprintf(stderr, "\t/MIN\t start minimized\n");
  264.   fprintf(stderr, "\t/N\t don't start indirectly through command processor\n");
  265.   fprintf(stderr, "\t/PGM\t the next argument is the program name\n");
  266.   fprintf(stderr, "\t/PM\t start a PM program\n");
  267.   fprintf(stderr, "\t/POS=x,y,x1,y1\t specify window position and size\n");
  268.   fprintf(stderr, "\t/SF\t read the specified dos settings file\n");
  269.   fprintf(stderr, "\t/WIN\t start a windowed session\n");
  270.   fprintf(stderr, "\t/WAIT\t doesn't work\n");
  271.   exit(1);
  272. }
  273.